package com.avecsys.quotemanager.mbeans.menu;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;

import com.avecsys.quotemanager.pojos.menu.MenuItemEntity;
import com.avecsys.quotemanager.pojos.menu.RootMenuEntity;
import com.avecsys.quotemanager.pojos.menu.SubMenuEntity;
import com.avecsys.quotemanager.service.menu.MenuManagerService;
import com.icesoft.faces.component.tree.Tree;
import com.icesoft.faces.context.effects.JavascriptContext;

/**
 * @author Samsundar.K
 * 
 */
public class MenuTreeMBean {

	private List<DefaultTreeModel> subMenuList = new ArrayList<DefaultTreeModel>();

	private MenuManagerService menuManagerService;

	private boolean adminVisible = false;

	private String headerMenu = "HEADER_MENU";

	private String subMenu = "SUB_MENU";

	// tree default model, used as a value for the tree component
	private DefaultTreeModel model;

	// default node icons for xp thme
	private static final String XP_BRANCH_CONTRACTED_ICON = "../../xmlhttp/css/rime/css-images/tree_folder_open.gif";

	private static final String XP_BRANCH_EXPANDED_ICON = "../../xmlhttp/css/rime/css-images/tree_folder_close.gif";

	private static final String XP_BRANCH_LEAF_ICON = "../../xmlhttp/css/rime/css-images/tree_document.gif";

	public MenuTreeMBean() {
	}

	/**
	 * Expanding the tree on folder click
	 * 
	 * @param actionEvent
	 */
	public void adminTreeClick(ActionEvent actionEvent) {
		// Fetching the Folder nodes from the from Root object
		FacesContext context = FacesContext.getCurrentInstance();
		String urlLink = (String) context.getExternalContext()
				.getRequestParameterMap().get("urlLink");
		if (urlLink != null && !urlLink.equals("#")
				&& urlLink.endsWith(".faces")) {
			JavascriptContext.addJavascriptCall(
					FacesContext.getCurrentInstance(), "updateFrame('"
							+ urlLink + "');");
		}
		if (actionEvent.getComponent().getParent().getParent().getParent() instanceof Tree) {
			Tree source = (Tree) actionEvent.getComponent().getParent()
					.getParent().getParent();
			DefaultMutableTreeNode currentNode = source.getCurrentNode();
			UrlNodeUserObject rootObject = new UrlNodeUserObject(currentNode);
			UrlNodeUserObject urlNodeUserObject = (UrlNodeUserObject) rootObject
					.getWrapper().getUserObject();
			if (urlNodeUserObject.isExpanded()) {
				urlNodeUserObject.setExpanded(false);
			} else {
				urlNodeUserObject.setExpanded(true);
			}
		}
	}

	/**
	 * @return the headerMenu
	 */
	public String getHeaderMenu() {
		return headerMenu;
	}

	/**
	 * @return the menuManagerService
	 */
	public MenuManagerService getMenuManagerService() {
		return menuManagerService;
	}

	/**
	 * This method checks the User Session
	 */
	/*
	 * public void checkUserSession() { try { HttpSession httpSession =
	 * (HttpSession) FacesContext
	 * .getCurrentInstance().getExternalContext().getSession(true); UsersEntity
	 * usersEntity = (UsersEntity) httpSession
	 * .getAttribute(CommonApplicationConstants.USER_SESSION_ID); if
	 * (usersEntity != null && usersEntity.getUserTypeId() == 1) {
	 * setAdminVisible(true); } else { setAdminVisible(false); } } catch
	 * (Exception e) { e.printStackTrace(); } }
	 */

	/**
	 * Gets the tree's default model.
	 * 
	 * @return tree model.
	 */
	public DefaultTreeModel getModel() {
		return model;
	}

	/**
	 * @return the subMenu
	 */
	public String getSubMenu() {
		return subMenu;
	}

	/**
	 * @return List<DefaultTreeModel>
	 */
	public List<DefaultTreeModel> getSubMenuList() {
		return subMenuList;
	}

	/**
	 * Initializes the admin menu
	 */
	public String initilizeAdminMenu() {
		subMenuList.clear();
		List<RootMenuEntity> rootMenuEntityList = menuManagerService
				.getAllMenus();
		RootMenuEntity rootMenuEntity = rootMenuEntityList.iterator().next();

		List<SubMenuEntity> subMenusSet = rootMenuEntity.getSubMenuEntityList();
		for (SubMenuEntity subMenuEntity : subMenusSet) {
			// create root node with its children expanded
			DefaultMutableTreeNode rootTreeNode = new DefaultMutableTreeNode();
			UrlNodeUserObject rootObject = new UrlNodeUserObject(rootTreeNode);
			rootObject.setHeaderMenu(true);
			rootObject.setMenuType(headerMenu);
			rootObject.setAction("onMouseClick");
			rootObject.setText(subMenuEntity.getSubMenuName());
			rootObject.setTarget("_self");
			rootObject.setUrl("#");
			rootObject.setExpanded(false);
			rootObject.setBranchContractedIcon(XP_BRANCH_CONTRACTED_ICON);
			rootObject.setBranchExpandedIcon(XP_BRANCH_EXPANDED_ICON);
			rootObject.setLeafIcon(XP_BRANCH_LEAF_ICON);

			rootTreeNode.setUserObject(rootObject);

			model = new DefaultTreeModel(rootTreeNode);
			subMenuList.add(new DefaultTreeModel(rootTreeNode));

			// Adding Menu Items for Sub Menu
			Set<MenuItemEntity> menuItemSet = subMenuEntity.getMenuItems();
			for (MenuItemEntity menuItemEntity : menuItemSet) {
				DefaultMutableTreeNode branchNode = new DefaultMutableTreeNode();
				UrlNodeUserObject branchObject = new UrlNodeUserObject(
						branchNode);
				branchObject.setHeaderMenu(false);
				rootObject.setMenuType(subMenu);
				branchObject.setText(menuItemEntity.getMenuItemName());
				branchObject.setUrl(menuItemEntity.getLinkPath());
				branchObject.setTarget("contentFrame");
				branchObject.setBranchContractedIcon(XP_BRANCH_CONTRACTED_ICON);
				branchObject.setBranchExpandedIcon(XP_BRANCH_EXPANDED_ICON);
				branchObject.setLeafIcon(XP_BRANCH_LEAF_ICON);
				branchObject.setLeaf(true);
				branchNode.setUserObject(branchObject);
				rootTreeNode.add(branchNode);
			}

		}
		return null;
	}

	/**
	 * @return the adminVisible
	 */
	public boolean isAdminVisible() {
		return adminVisible;
	}

	/**
	 * @return String
	 */
	public String onMouseClick() {
		System.out.println("Clicked the mouse");
		return ("");
	}

	/**
	 * @param adminVisible
	 *            the adminVisible to set
	 */
	public void setAdminVisible(boolean adminVisible) {
		this.adminVisible = adminVisible;
	}

	/**
	 * @param headerMenu
	 *            the headerMenu to set
	 */
	public void setHeaderMenu(String headerMenu) {
		this.headerMenu = headerMenu;
	}

	/**
	 * @param menuManagerService
	 *            the menuManagerService to set
	 */
	public void setMenuManagerService(MenuManagerService menuManagerService) {
		this.menuManagerService = menuManagerService;
		// checkUserSession();
		adminVisible = true;
		if (adminVisible) {
			initilizeAdminMenu();
		}
	}

	/**
	 * @param model
	 */
	public void setModel(DefaultTreeModel model) {
		this.model = model;
	}

	/**
	 * @param subMenu
	 *            the subMenu to set
	 */
	public void setSubMenu(String subMenu) {
		this.subMenu = subMenu;
	}

	/**
	 * @param subMenuList
	 */
	public void setSubMenuList(List<DefaultTreeModel> subMenuList) {
		this.subMenuList = subMenuList;
	}

}
